// 本地存储管理工具

import { STORAGE_KEYS, UserProgress, Scene, Word, Achievement } from '../types';

class StorageManager {
  // 获取用户进度
  getUserProgress(): UserProgress {
    const stored = localStorage.getItem(STORAGE_KEYS.USER_PROGRESS);
    if (stored) {
      return JSON.parse(stored);
    }
    
    // 默认进度
    const defaultProgress: UserProgress = {
      totalWordsLearned: 0,
      totalScenesCreated: 0,
      totalStudyTime: 0,
      currentStreak: 0,
      longestStreak: 0,
      achievements: this.getDefaultAchievements(),
      level: 1,
      experience: 0
    };
    
    this.saveUserProgress(defaultProgress);
    return defaultProgress;
  }

  // 保存用户进度
  saveUserProgress(progress: UserProgress): void {
    localStorage.setItem(STORAGE_KEYS.USER_PROGRESS, JSON.stringify(progress));
  }

  // 获取已保存的场景
  getSavedScenes(): Scene[] {
    const stored = localStorage.getItem(STORAGE_KEYS.SAVED_SCENES);
    return stored ? JSON.parse(stored) : [];
  }

  // 保存场景
  saveScene(scene: Scene): void {
    const scenes = this.getSavedScenes();
    const existingIndex = scenes.findIndex(s => s.id === scene.id);
    
    if (existingIndex >= 0) {
      scenes[existingIndex] = scene;
    } else {
      scenes.push(scene);
    }
    
    localStorage.setItem(STORAGE_KEYS.SAVED_SCENES, JSON.stringify(scenes));
  }

  // 删除场景
  deleteScene(sceneId: string): void {
    const scenes = this.getSavedScenes().filter(s => s.id !== sceneId);
    localStorage.setItem(STORAGE_KEYS.SAVED_SCENES, JSON.stringify(scenes));
  }

  // 获取已学习的单词
  getLearnedWords(): Word[] {
    const stored = localStorage.getItem(STORAGE_KEYS.LEARNED_WORDS);
    return stored ? JSON.parse(stored) : [];
  }

  // 保存已学习的单词
  saveLearnedWord(word: Word): void {
    const words = this.getLearnedWords();
    const existingIndex = words.findIndex(w => w.id === word.id);
    
    if (existingIndex >= 0) {
      words[existingIndex] = word;
    } else {
      words.push(word);
    }
    
    localStorage.setItem(STORAGE_KEYS.LEARNED_WORDS, JSON.stringify(words));
  }

  // 获取设置
  getSettings(): any {
    const stored = localStorage.getItem(STORAGE_KEYS.SETTINGS);
    return stored ? JSON.parse(stored) : {
      voiceEnabled: true,
      language: 'en',
      autoPlay: true,
      difficulty: 'intermediate'
    };
  }

  // 保存设置
  saveSettings(settings: any): void {
    localStorage.setItem(STORAGE_KEYS.SETTINGS, JSON.stringify(settings));
  }

  // 获取默认成就
  private getDefaultAchievements(): Achievement[] {
    return [
      {
        id: 'first_word',
        title: '第一个单词',
        description: '学习第一个单词',
        icon: '🌟',
        progress: 0,
        maxProgress: 1
      },
      {
        id: 'first_scene',
        title: '第一个场景',
        description: '创建第一个学习场景',
        icon: '🎨',
        progress: 0,
        maxProgress: 1
      },
      {
        id: 'pronunciation_master',
        title: '发音大师',
        description: '连续10次发音正确',
        icon: '🎤',
        progress: 0,
        maxProgress: 10
      },
      {
        id: 'word_collector',
        title: '单词收集家',
        description: '学习100个单词',
        icon: '📚',
        progress: 0,
        maxProgress: 100
      },
      {
        id: 'scene_explorer',
        title: '场景探索者',
        description: '创建20个不同场景',
        icon: '🌍',
        progress: 0,
        maxProgress: 20
      }
    ];
  }

  // 清空所有数据
  clearAllData(): void {
    Object.values(STORAGE_KEYS).forEach(key => {
      localStorage.removeItem(key);
    });
  }
}

export const storageManager = new StorageManager();